package com.yy.young.travel.web;

import com.yy.young.common.util.CommonUtil;
import com.yy.young.common.util.Result;
import com.yy.young.common.util.StringUtils;
import com.yy.young.dal.util.Page;
import com.yy.young.interfaces.log.annotation.Log;
import com.yy.young.travel.model.Source;
import com.yy.young.travel.model.SourceGrade;
import com.yy.young.travel.service.ISourceGradeService;
import com.yy.young.travel.service.ISourceService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 来源服务
 * Created by hua on 2018-03-21.
 */
@Controller
@RequestMapping("/travel/source")
public class SourceController {

    @Resource(name="sourceService")
    ISourceService service;



    /**
     * 获取数据列表
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询来源列表")
    @RequestMapping("/getSourceList")
    @ResponseBody
    public Object getSourceList(Source source, HttpServletRequest request) throws Exception{
        List<Source> list = service.getSourceList(source);
        return new Result(list);
    }

    /**
     * 获取分页数据
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("分页查询来源列表")
    @RequestMapping("/getSourcePage")
    @ResponseBody
    public Object getSourcePage(Source source, HttpServletRequest request) throws Exception{
        Page page = CommonUtil.getPageFromRequest(request);
        List<Source> list = service.getSourcePage(source, page);
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("查询来源")
    @RequestMapping("/getSource")
    @ResponseBody
    public Object getSource(String id, HttpServletRequest request) throws Exception{
        Source source = service.getSource(id);
        return new Result(source);
    }

    /**
     * 新增
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增来源")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(Source source, HttpServletRequest request) throws Exception {
        int num = service.insertSource(source);
        return new Result();
    }
    /**
     * 新增资源和得分
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("新增资源和得分")
    @RequestMapping("/insertSourceAndGrade")
    @ResponseBody
    public Object insert2(Source source, String gradeStr, HttpServletRequest request) throws Exception{
        if (StringUtils.isBlank(source.getId())){
            source.setId(CommonUtil.getUUID());
        }
        List<SourceGrade> list = new ArrayList<SourceGrade>();//得分数据
        if (StringUtils.isNotBlank(gradeStr)){//当得分信息有效时,解析得分数据生成list
            String[] arr = gradeStr.split(",");
            if (arr != null){
                for (String str : arr){
                    if (StringUtils.isNotBlank(str)){
                        String[] arr2 = str.split("@");//拆分
                        if (arr2 != null && arr2.length == 2){
                            SourceGrade sg = new SourceGrade();
                            sg.setId(CommonUtil.getUUID());
                            sg.setSourceId(source.getId());
                            sg.setItemId(arr2[0]);
                            sg.setGrade(StringUtils.isBlank(arr2[1]) ? null : Integer.parseInt(arr2[1]));
                            list.add(sg);
                        }
                    }
                }
            }
        }
        int num = service.insertSourceAndGrade(source, list);
        return new Result();
    }
    /**
     * 修改
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改来源")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Source source, HttpServletRequest request) throws Exception {
        int num = service.updateSource(source);
        return new Result();
    }

    /**
     * 修改并更新得分
     * @param source
     * @param request
     * @return
     * @throws Exception
     */
    @Log("修改资源并更新得分")
    @RequestMapping("/updateSourceAndGrade")
    @ResponseBody
    public Object update2(Source source, String gradeStr, HttpServletRequest request) throws Exception{
        int num = service.updateSource(source);
        List<SourceGrade> list = new ArrayList<SourceGrade>();//得分数据
        if (StringUtils.isNotBlank(gradeStr)){//当得分信息有效时,解析得分数据生成list
            String[] arr = gradeStr.split(",");
            if (arr != null){
                for (String str : arr){
                    if (StringUtils.isNotBlank(str)){
                        String[] arr2 = str.split("@");//拆分
                        if (arr2 != null && arr2.length == 2){
                            SourceGrade sg = new SourceGrade();
                            sg.setId(CommonUtil.getUUID());
                            sg.setSourceId(source.getId());
                            sg.setItemId(arr2[0]);
                            System.out.println(arr2[1]);
                            sg.setGrade(StringUtils.isBlank(arr2[1]) ? null : Integer.parseInt(arr2[1]));
                            list.add(sg);
                        }
                    }
                }
            }
        }
        service.updateSourceAndGrade(source, list);
        return new Result();
    }

    /**
     * 删除
     * @param ids
     * @param id
     * @param request
     * @return
     * @throws Exception
     */
    @Log("删除来源")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) throws Exception{
        Result result = new Result();
        if(StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            int num = service.deleteSource(idArr);
        }else if(StringUtils.isNotBlank(id)){
            int num = service.deleteSource(id);
        }else{
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

}